package cn.datawin.cmppclient.sender.util;

import cn.datawin.cmppclient.sender.entity.Provider;
import cn.datawin.cmppclient.sender.entity.ProviderStatus;
import cn.datawin.cmppclient.sender.provider.TcpMessageProvider;
import cn.datawin.cmppclient.sender.service.CmppService;
import cn.datawin.sms.cmpp2.SmsDetailLog;
import cn.hutool.extra.spring.SpringUtil;
import com.drondea.sms.channel.ChannelSession;
import com.drondea.sms.conf.ClientSocketConfig;
import com.drondea.sms.message.IMessage;
import com.drondea.sms.message.MessageProvider;
import com.drondea.sms.session.AbstractClientSessionManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;

/**
 * 挂起数据处理
 **/
@Slf4j
public class SenderUtil {

    /**
     * 将submit转换成要提交的tcp协议对象
     *
     * @param provider
     * @param submit
     * @param channelSession
     * @return
     */
    public static IMessage getTcpSendMsg(Provider provider, SmsDetailLog submit, ChannelSession channelSession) {
        if (!channelSession.getChannel().isActive()) {
//            log.debug("getTcpSendMsg: {} is not active", provider.getNo());
            return null;
        }
        // 设置发送器Ip
//        submit.setSenderLocalIP(provider.getSender_Local_IP());

//        AbstractTcpSenderService service = SpringContextUtil
//                .getBean(ProtocolType.getSenderServiceClasze(provider.getProtocol_Type_Code()));
        return SpringUtil.getBean(CmppService.class).getSubmitMessage(provider, submit, channelSession);
    }

    /**
     * 通道是否是挂起状态
     *
     * @param channel
     * @return
     */
    public static boolean isChannelHold(Provider channel) {
        return channel.getStatus_Code().equalsIgnoreCase(ProviderStatus.HOLD.toString());
    }

    /**
     * 重新调整连接的数量、速度和窗口大小
     *
     * @param sessionManager
     * @param provider
     */
    public static void resetChannelParam(AbstractClientSessionManager sessionManager, Provider provider) {

        resetProviderChannel(sessionManager, provider);

        ClientSocketConfig socketConfig = (ClientSocketConfig) sessionManager.getSocketConfig();
        //http的是null
        if (socketConfig == null) {
            return;
        }

        short channelSize = ObjectUtils.defaultIfNull(provider.getMax_Concurrent_Total(), 1).shortValue();
        //调整连接数
        if (socketConfig.getChannelSize() != channelSize) {
            socketConfig.setChannelSize(channelSize);
        }

        //发送限速
        Integer submitSpeed = ObjectUtils.defaultIfNull(provider.getSubmit_Speed(), 200);
        if (socketConfig.getQpsLimit() != submitSpeed) {
            sessionManager.resetSpeed(submitSpeed);
        }

        //调整窗口
        // TODO: 2022/11/29
        int windowSize = 32;
        if (socketConfig.getWindowSize() != windowSize) {
            sessionManager.resetWindowSize(windowSize);
        }
    }

    /**
     * 调整messageProvider的channel属性
     *
     * @param sessionManager
     * @param channel
     */
    private static void resetProviderChannel(AbstractClientSessionManager sessionManager, Provider channel) {
        MessageProvider messageProvider = sessionManager.getMessageProvider();
        //状态改变
        if (!(messageProvider instanceof TcpMessageProvider)) {
            return;
        }
        Provider channelOld = ((TcpMessageProvider) messageProvider).getProvider();
        if (channelOld.getStatus_Code().equals(channel.getStatus_Code())) {
            return;
        }
        //更改通道状态
        channelOld.setStatus_Code(channel.getStatus_Code());
    }

    /*public static void saveHoldMessage(Submit submit, Provider channel) {
        //2021.01.28 通道挂起状态，直接返回失败，消费掉mq
        submit.setChannelNo(channel.getNo());
        // 提交时间
        submit.setSubmitDate(submit.getSubmitDate() == null ? new Date() : submit.getSubmitDate());
        // 响应时间
        submit.setSubmitResponseDate(new Date());
        submit.setSubmitDescription("取消");

        // TODO: 2022/11/29
//        SMSUtil.saveFailedReports(submit, true);
    }*/
}
